<template>
  <div id="app"
       class="app-wrapper">
    <router-view></router-view>
  </div>
</template>

<script>
let Wi = window;
import "../src/assets/scss/index.scss"
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'App',
  components: {

  },
  created() {
    this.loadNetCa();
  },
  methods: {
    loadNetCa() {
      const $script = document.createElement('script');
      $script.src = '../static/netcawebsocket.js';
      document.body.appendChild($script);
      $script.onload = () => {
        setTimeout(() => {
          this.netcs();
        }, 2000);
      }
    },
    netcs() {
      if (Wi.NetcaPKI) {
        Wi.NetcaPKI.CheckLocalServerOk()
          .Then((res) => {
            let _delayTime = 0; //延迟时间，单位是毫秒，默认是0毫秒
            let params = {
              delayTime: _delayTime, //延迟时间，默认为0毫秒
            };
            Wi.NetcaPKI.monitorDevice(params)
              .Then((res) => {
                if (res != null) {
                  if (this.loginType === 4) {
                    if (res.pullOut === 1 && res.sn === this.certsn) {
                      //如果拔出的ukey是当前登录账号
                      // pullOutcb();
                    } else if (res.pullOut === 0 && res.sn === this.certsn) {
                      //如果插入的ukey是当前登录账号
                      this.$store.commit('SET_PULLON', false);
                    }
                  } else if (this.loginType === 1) {
                    this.$store.commit('SET_CERTFLAG');
                  }
                }
                //防刷新导致websocket数据丢失
                if (res === null && this.loginType === 4) {
                  this.verifyPwd();
                }
              })
              .Catch(function (res) {
                if (this.loginType != 0 && this.loginType != 2) {
                  this.$message.error(res.msg);
                }
              });
          })
          .Catch(() => { });
      } else {
        setTimeout(() => {
          this.netcs();
        }, 1000);
      }
    },
  },
  verifyPwd() {
    let _pwd = this.getUkeyId;
    let _sn = this.certsn;
    let _type = this.deviceType;
    let param = {
      pwd: _pwd,
      sn: _sn,
      type: Number(_type),
    };
    setTimeout(() => {
      Wi.NetcaPKI.verifyKeyPwd(param)
        .Then(function (res) {
          window.deviceTimer = setInterval(() => {
            Wi.NetcaPKI.verifyKeyPwd(param)
          }, 1800000)
        })
        .Catch(function (res) {
          console.log('--res',res);
          this.$message.error(res.msg + '即将返回登录页');
          if (res.status === -1600) {
            clearCookie();
            // toLogin(store);
            this.$store.commit('SET_PULLON', false);
            setTimeout(() => {
              window.location.href = `${window.location.href.split('#')[0]}#/login`;
            }, 2000);
          }
        });
    });
  },
  computed: {
    ...mapState({
      certsn: state => state.userData.certsn,
      loginType: state => state.userData.loginType,
      deviceType: state => state.userData.deviceType
    }),
    ...mapGetters('userData', {
      getUkeyId: 'getUkeyId'
    })
  },
}
</script>

<style lang="scss" scoped>
</style>
